package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 菜品模块
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    //分页+条件查询
    @GetMapping("/dish/page")
    public ResultInfo findByPage(
            @RequestParam(name = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String name) {//接收参数
        //调用service
        Page<Dish> page = dishService.findByPage(pageNum, pageSize, name);
        return ResultInfo.success(page);

    }

    //新增菜品
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish) {
        //接收post请求参数
        //调用service新增
        dishService.save(dish);
        //返回成功
        return ResultInfo.success();
    }

    //修改菜品
    @PutMapping("/dish")
    public ResultInfo updateById(@RequestBody Dish dish) {// 1.接收put请求参数
        //调用service修改
        dishService.updateById(dish);
        //返回结果
        return ResultInfo.success();
    }
//回显
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable long id) {
        //接收路径参数
        //2调用service查询
        Dish dish = dishService.findById(id);
        // 3.返回结果
        return ResultInfo.success(dish);
    }

    //删除
    @DeleteMapping("/dish")
    public ResultInfo deleteBatchchIds(@RequestParam List<Long> ids) {
        //调用service
        dishService.deleteBatchIds(ids);
        //返回结果
        return ResultInfo.success();


    }

    //菜品起售,停售
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {//接收参数
        //        //        //调用service
        dishService.updateStatus(status, ids);
        //返回结果
        return ResultInfo.success();
    }

    // 根据分类查询菜品列表
    @GetMapping("/dish/list")
    public ResultInfo findListByCategoryId(Long categoryId ,String name) {//接收参数
        //2调用service
        List<Dish> dishList = dishService.findListByCategoryId(categoryId,name);
        //返回结果
        return ResultInfo.success(dishList);

    }
}